package com.haliyoo.adhere.handler;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.haliyoo.adhere.R;
import com.haliyoo.adhere.activity.CreativeActivity;
import com.haliyoo.adhere.activity.HallActivity;
import com.haliyoo.adhere.activity.LoginActivity;
import com.haliyoo.adhere.bean.WeiboBean;
import com.haliyoo.adhere.controller.AdUserConstant;
import com.haliyoo.adhere.controller.OAuthConstant;
import com.haliyoo.adhere.handler.DraftsHandler.WeiboList;
import com.haliyoo.adhere.util.DateUtilsDef;
import com.haliyoo.adhere.widget.AsyncImageLoader;
import com.haliyoo.adhere.widget.DialogListener;
import com.haliyoo.adhere.widget.LocalWeiboLayout;
import com.haliyoo.adhere.widget.PromptDialog;
import com.haliyoo.adhere.widget.WeiboLayout;
import com.haliyoo.adhere.widget.AsyncImageLoader.ImageCallback;

import weibo4android.Status;
import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class InviteFriendsHandler {
	private static String TAG = "InviteFriendsHandler";
	static Context context = null;
	public static WeiboList weiboList = null;

	/**
	 * 内部类，实现微博数据获取
	 * @author wation.haliyoo
	 *
	 */
	class WeiboList extends LocalWeiboLayout{

		WeiboList(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			if (HallActivity.getCheckedId() != R.id.radio_invite) {
				return;
			}
		}

		@Override
		public List<WeiboBean> onGetWeiboList() {
			// TODO Auto-generated method stub
			if (HallActivity.getCheckedId() != R.id.radio_invite) {
				return null;
			}
			
			if (LoginActivity.getUser() == null) {
				Toast.makeText(context, 
						R.string.STR_NONE_USER, 
						Toast.LENGTH_SHORT).show();
			} else {
				List<WeiboBean> wbList = new ArrayList<WeiboBean>();

//				/// 从AdHere服务器下载广告
//				AdUserConstant.getInstance().list(AdUserConstant.getInstance().user, 
//						AdUserConstant.getInstance().sign);
//				
//				/// 清空原有list所有项
//				wbList.clear();
//				
//				for (int i=0; i<AdUserConstant.getInstance().inviteList.size(); i++) {
//					
//					WeiboBean wb = AdUserConstant.getInstance().inviteList.get(i);
//					
//					wbList.add(wb);
//				}
				
				return wbList;
			}
			
			return null;
		}

		@Override
		public void onShowWeiboDetail(AdapterView<?> arg0, View view, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			if (HallActivity.getCheckedId() != R.id.radio_invite) {
				return;
			}
			
			WeiboBean weiboBean = (WeiboBean) view.getTag();
			if (weiboBean != null) {
				Intent intent = new Intent(context,
						CreativeActivity.class);
				Bundle b = new Bundle();
				b.putString("id", weiboBean.getId());
				intent.putExtras(b);
				context.startActivity(intent);
			}
		}

		@Override
		public void onShowWeiboDetail(WeiboBean weiboBean) {
			// TODO Auto-generated method stub
			if (HallActivity.getCheckedId() != R.id.radio_invite) {
				return;
			}

			if (weiboBean != null) {
				Intent intent = new Intent(context,
						CreativeActivity.class);
				Bundle b = new Bundle();
				b.putString("id", weiboBean.getId());
				intent.putExtras(b);
				context.startActivity(intent);
			}
		}

		@Override
		public void onFinishedPublish(WeiboBean weiboBean) {
			// TODO Auto-generated method stub
			if (HallActivity.getCheckedId() != R.id.radio_invite) {
				return;
			}
			
		}
	}
	
	public InviteFriendsHandler(Context context) {
		if (HallActivity.getCheckedId() != R.id.radio_invite) {
			return;
		}
		
		this.context = context;

        LayoutInflater inflater = LayoutInflater.from(context);
        
		// / 设置标题栏
		RelativeLayout rlTitle = (RelativeLayout) ((Activity) context)
				.findViewById(R.id.main_title);

		View vTitle = inflater.inflate(R.layout.title_back_delete, null);
		rlTitle.removeAllViews();
		rlTitle.addView(vTitle);

		// / 设置内容区
		LinearLayout llContent = (LinearLayout) ((Activity) context)
				.findViewById(R.id.llContent);
		View vContent = inflater.inflate(R.layout.local_weibo_list, null);
		llContent.removeAllViews();
		llContent.addView(vContent);

		/**
		 * 返回键处理
		 */
		ImageButton ibBack = (ImageButton) ((Activity) context).findViewById(R.id.IMAGE_BUTTON_BACK);
		ibBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (HallActivity.getCheckedId() != R.id.radio_invite) {
					return;
				}

				// / 提示是否退出软件
				PromptDialog.show(DraftsHandler.context,
						((Activity) DraftsHandler.context)
								.getString(R.string.STR_EXIT_APP_REQUEST),
						((DialogListener) DraftsHandler.context), 0, false);
			}
		});

		/**
		 * 删除键处理
		 */
		ImageButton ibDelete = (ImageButton) ((Activity) context).findViewById(R.id.IMAGE_BUTTON_DELETE);
		ibDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (HallActivity.getCheckedId() != R.id.radio_invite) {
					return;
				}

				PromptDialog.show(DraftsHandler.context, DraftsHandler.context
						.getString(R.string.STR_DELETE_ALL_DRAFTS),
						(DialogListener) DraftsHandler.context, 1, false);
			}
		});
		
        TextView tvTitle = (TextView) ((Activity) context).findViewById(R.id.TEXT_VIEW_TITLE);
        tvTitle.setText(context.getString(R.string.STR_INVITE_FRIENDS));

		/// 加载微博列表
		if (weiboList == null) {
			weiboList = new WeiboList(context);
		}
		weiboList.refreshWeiboList();
	}
}
